package com.wsc.wsckeeper.activities;

import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;

import com.wsc.wsckeeper.R;
import com.wsc.wsckeeper.activities.fragments.AccountsFragment;
import com.wsc.wsckeeper.activities.fragments.CardsFragment;
import com.wsc.wsckeeper.enums.Mode;
import com.wsc.wsckeeper.enums.OrderBy;
import com.wsc.wsckeeper.listeners.TabsListener;
import com.wsc.wsckeeper.managers.PreferenceManager;
import com.wsc.wsckeeper.managers.ResourceManager;
import com.wsc.wsckeeper.managers.ScreenManager;
import com.wsc.wsckeeper.model.KModel;
import com.wsc.wsckeeper.model.KRuntimeModel;
import com.wsc.wsckeeper.model.entity.AccountE;
import com.wsc.wsckeeper.model.entity.CardE;

public class BaseTabActivity extends FragmentActivity {
	private ActionBar actionBar;
	public static final int CARDS_TAB = 0;
	public static final int ACCOUNTS_TAB = 1;

	private final int requestCode = 1001;
	private CardsFragment cardsFragment = new CardsFragment();
	private AccountsFragment accountsFragment = new AccountsFragment();

	private SubMenu menuCard;
	private SubMenu menuAccount;

	private ViewPager mViewPager;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_base_tab);

		FragmentPagerAdapter pagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
			@Override
			public Fragment getItem(int i) {
				switch (i) {
				case 0:
					return cardsFragment;
				}
				return accountsFragment;
			}

			@Override
			public int getCount() {
				return 2;
			}
		};

		actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(pagerAdapter);
		mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				actionBar.setSelectedNavigationItem(position);
			}
		});

		ActionBar.Tab cardsTab = actionBar.newTab().setIcon(R.drawable.card);
		ActionBar.Tab accountsTab = actionBar.newTab().setIcon(R.drawable.account);

		cardsTab.setTabListener(new TabsListener(actionBar, this, mViewPager));
		accountsTab.setTabListener(new TabsListener(actionBar, this, mViewPager));

		actionBar.addTab(cardsTab, CARDS_TAB);
		actionBar.addTab(accountsTab, ACCOUNTS_TAB);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();

		int i = actionBar.getSelectedTab().getPosition();
		switch (i) {
		case BaseTabActivity.CARDS_TAB:
			inflater.inflate(R.menu.menu_card, menu);
			menuCard = menu.getItem(0).getSubMenu();
			break;
		case BaseTabActivity.ACCOUNTS_TAB:
			inflater.inflate(R.menu.menu_account, menu);
			menuAccount = menu.getItem(0).getSubMenu();
			break;
		}
		return true;
	}

	private void refreshMenuItemImages(SubMenu menu, MenuItem item) {
		int size = menu.size();

		for (int i = 0; i < size; i++) {
			MenuItem menuItem = menu.getItem(i);
			menuItem.setIcon(ResourceManager.getDrawable(R.drawable.ic_empty));
		}

		OrderBy order = KModel.getInstance().getOrder();
		if (order.equals(OrderBy.ASC))
			item.setIcon(ResourceManager.getDrawable(R.drawable.ic_navigation_expand));
		else
			item.setIcon(ResourceManager.getDrawable(R.drawable.ic_navigation_collapse));
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case R.id.card_menu_number:
			refreshMenuItemImages(menuCard, item);
			cardsFragment.sort(CardE.FIELD_NUMBER);
			return true;
		case R.id.card_menu_created:
			refreshMenuItemImages(menuCard, item);
			cardsFragment.sort(CardE.FIELD_CREATED);
			return true;
		case R.id.card_menu_description:
			refreshMenuItemImages(menuCard, item);
			cardsFragment.sort(CardE.FIELD_DESCRIPTION);
			return true;
		case R.id.card_menu_currency:
			refreshMenuItemImages(menuCard, item);
			cardsFragment.sort(CardE.FIELD_FK_CURRENCY);
			return true;
		case R.id.account_menu_destination:
			refreshMenuItemImages(menuAccount, item);
			accountsFragment.sort(AccountE.FIELD_DESCRIPTION);
			return true;
		case R.id.account_menu_created:
			refreshMenuItemImages(menuAccount, item);
			accountsFragment.sort(AccountE.FIELD_CREATED);
			return true;
		case R.id.account_menu_login:
			refreshMenuItemImages(menuAccount, item);
			accountsFragment.sort(AccountE.FIELD_LOGIN);
			return true;

		case R.id.menu_add:
			switch (actionBar.getSelectedTab().getPosition()) {
			case CARDS_TAB:
				KModel.getInstance().setCardMode(Mode.ADD);
				ScreenManager.getInstance().forward(this, CardActivity.class);
				break;
			case ACCOUNTS_TAB:
				KModel.getInstance().setAccountMode(Mode.ADD);
				ScreenManager.getInstance().forward(this, AccountActivity.class);
				break;
			}
			return true;
		case R.id.menu_export:
			ScreenManager.getInstance().forward(this, ExportActivity.class);
			return true;
		case R.id.menu_import:
			ScreenManager.getInstance().forwardForResult(this, ImportActivity.class, requestCode);
			return true;
		case R.id.menu_settings:
			ScreenManager.getInstance().forward(this, SettingsActivity.class);
			return true;
		case android.R.id.home:
		case R.id.menu_exit:
			finish();
			return true;
		}
		return false;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (this.requestCode == requestCode && resultCode == RESULT_OK) {
			KRuntimeModel.getInstance().cleanData();
			cardsFragment.initValues();
			accountsFragment.initValues();
		}
	}

	@Override
	public void onBackPressed() {
		if (PreferenceManager.getInstance().getNeedLogin()) {
			super.onBackPressed();
		} else {
			moveTaskToBack(true);
		}
	}
}